package com.william.jdbcplus.boot.test.controller;

import com.william.jdbcplus.boot.test.dao.SysDictDao;
import com.william.jdbcplus.boot.test.dto.R;
import com.william.jdbcplus.boot.test.entity.SysDict;
import com.william.jdbcplus.boot.test.vo.CheckVo;
import com.william.jdbcplus.boot.test.vo.DictQueryVO;
import com.william.jdbcplus.boot.test.vo.DictVO;
import com.william.jdbcplus.boot.test.vo.TreeSelectVO;
import com.william.jdbcplus.common.page.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author niliwei
 */
@Api(value = "字典测试")
@RestController
@AllArgsConstructor
@Validated
@RequestMapping("/dict")
public class DictController {
    private SysDictDao sysDictDao;

    /**
     * 重复检查SQL
     *
     * @return
     */
    @ApiOperation(value = "重复检查SQL")
    @GetMapping(value = "/duplicateCheckCountSql")
    R<Long> duplicateCheckCountSql() {
        CheckVo checkVo = new CheckVo();
        Long result = sysDictDao.duplicateCheckCountSql(checkVo);
        return R.data(result);
    }

    @ApiOperation(value = "添加时检查ID是否存在")
    @GetMapping(value = "/duplicateCheckCountSqlNoDataId")
    R<Long> duplicateCheckCountSqlNoDataId() {
        CheckVo checkVo = new CheckVo();
        Long result = sysDictDao.duplicateCheckCountSqlNoDataId(checkVo);
        return R.data(result);
    }

    @ApiOperation(value = "queryDictItemsByCode")
    @GetMapping(value = "/queryDictItemsByCode")
    R<List<DictVO>> queryDictItemsByCode() {
        String code = "123";
        List<DictVO> list = sysDictDao.queryDictItemsByCode(code);
        return R.data(list);
    }

    @ApiOperation(value = "queryTableDictItemsByCode")
    @GetMapping(value = "/queryTableDictItemsByCode")
    R<List<DictVO>> queryTableDictItemsByCode() {
        String table = "";
        String text = "";
        String code = "";
        List<DictVO> list = sysDictDao.queryTableDictItemsByCode(table, text, code);
        return R.data(list);
    }

    @ApiOperation(value = "queryTableDictItemsByCodeAndFilter")
    @GetMapping(value = "/queryTableDictItemsByCodeAndFilter")
    R<List<DictVO>> queryTableDictItemsByCodeAndFilter() {
        String table = "";
        String text = "";
        String code = "";
        String filterSql = "";
        List<DictVO> list = sysDictDao.queryTableDictItemsByCodeAndFilter(table, text, code, filterSql);
        return R.data(list);
    }

    @ApiOperation(value = "queryDictTextByKey")
    @GetMapping(value = "/queryDictTextByKey")
    R<String> queryDictTextByKey() {
        String code = "";
        String key = "";
        String result = sysDictDao.queryDictTextByKey(code, key);
        return R.data(result);
    }

    @ApiOperation(value = "queryTableDictTextByKey")
    @GetMapping(value = "/queryTableDictTextByKey")
    R<String> queryTableDictTextByKey() {
        String table = "";
        String text = "";
        String code = "";
        String key = "";
        String result = sysDictDao.queryTableDictTextByKey(table, text, code, key);
        return R.data(result);
    }

    @ApiOperation(value = "queryTableDictByKeys")
    @GetMapping(value = "/queryTableDictByKeys")
    R<List<DictVO>> queryTableDictByKeys() {
        String table = "";
        String text = "";
        String code = "";
        String[] keyArray = new String[]{
                "1", "2"
        };
        List<DictVO> list = sysDictDao.queryTableDictByKeys(table, text, code, keyArray);
        return R.data(list);
    }

    /**
     * 查询所有部门 作为字典信息 id -->value,departName -->text
     *
     * @return
     */
    @ApiOperation(value = "queryAllDepartBackDictModel")
    @GetMapping(value = "/queryAllDepartBackDictModel")
    R<List<DictVO>> queryAllDepartBackDictModel() {
        List<DictVO> result = sysDictDao.queryAllDepartBackDictModel();
        return R.data(result);
    }

    /**
     * 查询所有用户  作为字典信息 username -->value,realname -->text
     *
     * @return
     */
    @ApiOperation(value = "queryAllUserBackDictModel")
    @GetMapping(value = "/queryAllUserBackDictModel")
    R<List<DictVO>> queryAllUserBackDictModel() {
        List<DictVO> result = sysDictDao.queryAllUserBackDictModel();
        return R.data(result);
    }

    /**
     * 通过关键字查询出字典表
     *
     * @return
     */
    @ApiOperation(value = "queryTableDictItems")
    @GetMapping(value = "/queryTableDictItems")
    R<List<DictVO>> queryTableDictItems() {
        String table = "";
        String text = "";
        String code = "";
        String keyword = "";
        List<DictVO> result = sysDictDao.queryTableDictItems(table, text, code, keyword);
        return R.data(result);
    }

    /**
     * 根据表名、显示字段名、存储字段名 查询树
     *
     * @return
     */
    @ApiOperation(value = "queryTreeList")
    @GetMapping(value = "/queryTreeList")
    R<List<TreeSelectVO>> queryTreeList() {
        Map<String, String> query = new HashMap<>();
        String table = "";
        String text = "";
        String code = "";
        String pidField = "";
        String pid = "";
        String hasChildField = "";
        List<TreeSelectVO> result = sysDictDao.queryTreeList(query, table, text, code, pidField, pid, hasChildField);
        return R.data(result);
    }

    /**
     * 删除
     */
    @ApiOperation(value = "deleteOneById")
    @GetMapping(value = "/deleteOneById")
    R<String> deleteOneById() {
        String id = "";
        sysDictDao.deleteOneById(id);
        return R.ok();
    }

    /**
     * 查询被逻辑删除的数据
     *
     * @return
     */
    @ApiOperation(value = "queryDeleteList")
    @GetMapping(value = "/queryDeleteList")
    R<List<SysDict>> queryDeleteList() {
        List<SysDict> dicts = sysDictDao.queryDeleteList();
        return R.data(dicts);
    }

    /**
     * 修改状态值
     */
    @ApiOperation(value = "updateDictDelFlag")
    @GetMapping(value = "/updateDictDelFlag")
    R<String> updateDictDelFlag() {
        int delFlag = 1;
        String id = "1";
        sysDictDao.updateDictDelFlag(delFlag, id);
        return R.ok();
    }

    /**
     * 分页查询字典表数据
     */
    @ApiOperation(value = "queryDictTablePageList")
    @GetMapping(value = "/queryDictTablePageList")
    R<Page<DictVO>> queryDictTablePageList() {
        Page page = new Page(1, 10);
        DictQueryVO query = new DictQueryVO();
        Page<DictVO> resultPage = sysDictDao.queryDictTablePageList(page, query);
        return R.data(resultPage);
    }
}
